package pl.koziolekweb.maven.reporting.linecouter;

/*
 * Copyright 2001-2005 The Apache Software Foundation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Locale;

import org.apache.maven.doxia.siterenderer.Renderer;
import org.apache.maven.project.MavenProject;
import org.apache.maven.reporting.AbstractMavenReport;
import org.apache.maven.reporting.MavenReportException;

/**
 * Goal which touches a timestamp file.
 * 
 * @author bartlomiejk
 * @goal report
 * @phase site
 */
public class LineCountMojo extends AbstractMavenReport {

	/**
	 * Location of the file.
	 * 
	 * @parameter expression="${project.build.directory}/linecounter/"
	 * @required
	 */
	private File outputDirectory;

	/**
	 * @parameter expression="${project.build.sourceDirectory}"
	 * @required
	 */
	private File sourceDirectory;

	private long counter = 0;

	@Override
	protected void executeReport(Locale arg0) throws MavenReportException {
		if (!sourceDirectory.exists()) {
			return;
		}
		if (!outputDirectory.exists()) {
			outputDirectory.mkdirs();
		}
		if (counter != 0) {
			counter = 0;
		}
		countLines(sourceDirectory);
		File output = new File(outputDirectory.getAbsolutePath() + "/count");
		try {
			output.createNewFile();
			FileWriter fileWriter = new FileWriter(output);
			fileWriter.write(counter + "");
			fileWriter.close();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
		}
	}

	private void countLines(File sourceDirectory2) {
		for (File f : sourceDirectory2.listFiles()) {
			if (f.isDirectory()) {
				countLines(f); // rekurencja to zuo
			}
			if (f.getPath().endsWith(".java")) {
				try {
					BufferedReader input = new BufferedReader(new FileReader(f));
					getLog().info("Reading file: " + f.getAbsolutePath());
					try {
						while (input.readLine() != null) {
							counter++;
						}
						counter++; // last line
					} finally {
						input.close();
					}
				} catch (IOException ex) {
					ex.printStackTrace();
				}
			}
		}
	}

	@Override
	protected String getOutputDirectory() {
		return outputDirectory.getAbsolutePath();
	}

	@Override
	protected MavenProject getProject() {
		return null;
	}

	@Override
	protected Renderer getSiteRenderer() {
		return null;
	}

	@Override
	public String getDescription(Locale arg0) {
		return "Count lines in .java files.";
	}

	@Override
	public String getName(Locale arg0) {
		return "Line Counter Report";
	}

	@Override
	public String getOutputName() {
		return "lineCounter/index";
	}
}
